package com.jago.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

// 统计网站在线人数：统计session
public class OnlineCountListener implements HttpSessionListener {

    // 创建session监听
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        ServletContext context = se.getSession().getServletContext();
        Integer onlineCount = (Integer) context.getAttribute("onlineCount");
        if (onlineCount == null) {
            onlineCount = new Integer(0);
        } else {
            onlineCount = onlineCount + 1;
        }

        context.setAttribute("onlineCount", onlineCount);
    }

    // 销毁session监听
    @Override
    public void sessionDestroyed(HttpSessionEvent se) {

    }
}
